package com.edwin.websitebe.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edwin.websitebe.mapper.OrderMapper;
import com.edwin.websitebe.model.Game;
import com.edwin.websitebe.model.Order;
import com.edwin.websitebe.model.OrderGame;
import com.edwin.websitebe.service.GameService;
import com.edwin.websitebe.service.OrderGameService;
import com.edwin.websitebe.service.OrderService;
import com.edwin.websitebe.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private OrderGameService orderGameService;

    @Autowired
    private GameService gameService;

    @Override
    public Order getByCode(String code) {

        LambdaQueryWrapper<Order> wrapper = new QueryWrapper<Order>().lambda()
                .eq(Order::getCode, code);
        return this.getOne(wrapper);
    }

    @Override
    public List<Game> listGameByOrderCode(String code) {
        Order order = this.getByCode(code);
        if (order == null) {
            return Collections.emptyList();
        }

        LambdaQueryWrapper<OrderGame> wrapper = new QueryWrapper<OrderGame>().lambda()
                .eq(OrderGame::getOrderId, order.getId());
        List<OrderGame> orderGameList = orderGameService.list(wrapper);
        List<Long> gameIdList = orderGameList.stream()
                .map(OrderGame::getGameId)
                .distinct()
                .collect(Collectors.toList());

        LambdaQueryWrapper<Game> gameWrapper = new QueryWrapper<Game>().lambda()
                .in(Game::getId, gameIdList)
                .orderByAsc(Game::getGameCode);


        return gameService.list(gameWrapper);
    }



}
